<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline"  @keyup.enter.native="searchQuery">
        <a-row :gutter="12">
          <a-col :md="7" :sm="8">
            <a-form-item label="门店名称" :labelCol="{span: 6}" :wrapperCol="{span: 14, offset: 1}">
              <a-input placeholder="请输入门店名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <!-- <a-col :md="7" :sm="8">
            <a-form-item label="状态" :labelCol="{span: 6}" :wrapperCol="{span: 14, offset: 1}">
              <a-select v-model="queryParam.status" placeholder="请选择" default-value="null" :getPopupContainer="triggerNode => triggerNode.parentNode">
                <a-select-option value="">全部</a-select-option>
                <a-select-option value="0">在线</a-select-option>
                <a-select-option value="1">离线</a-select-option>
              </a-select>
            </a-form-item>
          </a-col> -->
          <a-col :md="7" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>

      <div class="table-operator" style="border-top: 5px">
        <a-button type="primary" @click="handleAdd">新增门店</a-button>
      </div>

      <a-table
        ref="table"
        rowKey="id"
        size="middle"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange">
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">
            <a-icon type="edit"/>
            编辑
          </a>
          <a-divider type="vertical"/>
          <a @click="clickewm(record)"><a-icon type="setting"/> 门店二维码下载</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() =>handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>

    <Operation-Modal ref="modalForm" @ok="modalFormOk"></Operation-Modal>

    <a-modal title="二维码" :dialog-style="{ top: '200px' }" :visible="removeVisible" @ok="clickCode()" okText="下载"  @cancel="() => (removeVisible = false)">
      <img
        style="width: 200;margin-left: 17%;"
        :src="qrCode"
      />
    </a-modal>
  </a-card>
</template>

<script>
import { filterObj } from '@/utils/util';
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import OperationModal from './modules/operationModal'

export default {
  name:'oper',
  mixins:[JeecgListMixin],
  components: {
    OperationModal,
  },
  data() {
    return {
      removeVisible: false,
      qrCode:'',
      queryParam:{
        current: 1,
        size: 10,
      },
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 120,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '门店ID',
          align: "center",
          dataIndex: 'storeId',
        },
        {
          title: '门店名称',
          align: "center",
          dataIndex: 'name',
        },
        {
          title: '营业时间',
          align: "center",
          dataIndex: 'openTime',
        },
        {
          title: '门店地址',
          align: "center",
          dataIndex: 'address',
        },
        // {
        //   title: 'IP地址',
        //   align: "center",
        //   dataIndex: 'ip',
        // },
        // {
        //   title: '设备名称',
        //   align: "center",
        //   dataIndex: 'deviceName',
        // },
        {
          title: '品牌',
          align: "center",
          dataIndex: 'brandName',
        },
        // {
        //   title: '设备状态',
        //   align: "center",
        //   dataIndex: 'statusText',
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          scopedSlots: {customRender: 'action'},
        }
      ],
      url: {
        list: "/backend/coffee/pageStoreVo",
        delete: "/backend/coffee/delStoreById",
      },
    }
  },
  created() {

  },
  methods: {
    getQueryParams() {
      var param = Object.assign({}, this.queryParam);
      param.current = this.ipagination.current;
      param.pageSize = this.ipagination.pageSize;
      return filterObj(param);
    },
    clickewm(record) {
      setTimeout(() => {
        this.removeVisible = true
      }, 1000);
      this.qrCode = record.qrCode
    },
    clickCode(record) {
      this.removeVisible = false
      this.downloadIamge(this.qrCode, '二维码');
    },
    downloadIamge(imgsrc, name) {
      //下载图片地址和图片名
      var image = new Image();
      image.setAttribute("crossOrigin", "anonymous");
      image.onload = function() {
        var canvas = document.createElement("canvas");
        canvas.width = image.width;
        canvas.height = image.height;
        var context = canvas.getContext("2d");
        context.drawImage(image, 0, 0, image.width, image.height);
        var url = canvas.toDataURL("image/png");

        var a = document.createElement("a");
        var event = new MouseEvent("click");
        a.download = name || "photo";
        a.href = url; 
        a.dispatchEvent(event);
      };
      image.src = imgsrc;
    },
  }
}
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>